package com.sgcc.nrxt.crmdemo.result;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sgcc.nrxt.crmdemo.constant.ProfessionType;
import com.sgcc.nrxt.crmdemo.constant.ResponseCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

@ApiModel(value = "SimpleResponseBody", description = "华云接口返回简易类型")
@Data
public class SimpleResponseBody<T> implements Serializable {

    @ApiModelProperty(value = "响应状态码", allowableValues = "10", example = "000000", required = true)
    protected String status;

    @ApiModelProperty(value = "错误信息", allowableValues = "200", example = "success")
    protected String errors;

    @ApiModelProperty(value = "错误信息", allowableValues = "200", example = "success")
    protected String message;

    @ApiModelProperty(value = "本次请求处理的总记录数")
    protected int count;

    @ApiModelProperty(value = "fields")
    @JsonIgnore
    @JSONField(serialize = false)
    private String fields;

    @ApiModelProperty(value = "fieldMap")
    @JsonIgnore
    @JSONField(serialize = false)
    private Map<String, String> fieldMap;

    @ApiModelProperty(value = "startID")
    @JsonIgnore
    @JSONField(serialize = false)
    private String startID;

    @ApiModelProperty(value = "返回结果是否排序")
    @JsonIgnore
    @JSONField(serialize = false)
    private boolean hasOrder;

    /**
     * data:需要返回的数据
     */
    @ApiModelProperty(value = "响应数据")
    protected T result;


    @JsonIgnore
    @JSONField(serialize = false)
    private ProfessionType professionType;

    @JsonIgnore
    @JSONField(serialize = false)
    private String modelId;


    /**
     * 创建一个新的实例 ResponseObject.
     */
    public SimpleResponseBody() {
    }

    public SimpleResponseBody(T result) {
        this.status = String.valueOf(ResponseCodeEnum.SUCCESS.getValue());
        this.result = result;
    }

    public SimpleResponseBody(String status, String errors) {
        this.status = status;
        this.errors = errors;
        this.message = errors;
    }

    public SimpleResponseBody(String status, String message, String errors) {
        this.status = status;
        this.errors = errors;
        this.message = message;
    }

    public SimpleResponseBody(String status, String errors, String startId, String fields, T result) {
        this.status = status;
        this.errors = errors;
        this.message = errors;
        this.startID = startId;
        this.fields = fields;
        this.result = result;
    }

    public SimpleResponseBody(String status, String errors, String startId, String fields, T result, int count) {
        this.status = status;
        this.errors = errors;
        this.message = errors;
        this.startID = startId;
        this.fields = fields;
        this.result = result;
        this.count = count;
    }

    public SimpleResponseBody(String status, String errors, String message, String startId, String fields, T result) {
        this.status = status;
        this.errors = errors;
        this.message = message;
        this.startID = startId;
        this.fields = fields;
        this.result = result;
    }

    public SimpleResponseBody(String status, String errors, String startId, Map<String, String> fieldMap, boolean isOrder, T result, ProfessionType professionType) {
        this.status = status;
        this.errors = errors;
        this.message = errors;
        this.startID = startId;
        this.fieldMap = fieldMap;
        this.result = result;
        this.hasOrder = isOrder;
        this.professionType = professionType;
    }

    public SimpleResponseBody(String status, String errors, String message, int count, String fields, Map<String, String> fieldMap, String startID,
                              boolean hasOrder, T result, ProfessionType professionType) {
        this.status = status;
        this.errors = errors;
        this.message = message;
        this.count = count;
        this.fields = fields;
        this.fieldMap = fieldMap;
        this.startID = startID;
        this.hasOrder = hasOrder;
        this.result = result;
        this.professionType = professionType;
    }

    public SimpleResponseBody(String status, String errors, String message, int count, String fields, Map<String, String> fieldMap, String startID,
                              boolean hasOrder, T result, ProfessionType professionType, String modelId) {
        this.status = status;
        this.errors = errors;
        this.message = message;
        this.count = count;
        this.fields = fields;
        this.fieldMap = fieldMap;
        this.startID = startID;
        this.hasOrder = hasOrder;
        this.result = result;
        this.professionType = professionType;
        this.modelId = modelId;
    }

    /**
     * *  构造成功调用结果对象，设置返回消息"success"
     *
     * @param data
     * @return
     */
    public static SimpleResponseBody<Object> success(Object data) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "success", "", "", data);
    }

    public static SimpleResponseBody<Object> success(int count, Object data) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "success", "", "", data, count);
    }

    public static SimpleResponseBody<Object> success(Object data, String message) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "", message, "", "", data);
    }

    public static SimpleResponseBody<Object> success(Object data, ProfessionType professionType) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "success", "success",
                0, null, null, null, false, data, professionType);
    }

    public static SimpleResponseBody<Object> success(Object data, ProfessionType professionType, String modelId) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "success", "success",
                0, null, null, null, false, data, professionType, modelId);
    }

    /**
     * *  构造成功调用结果对象，设置返回消息"success"
     *
     * @param startId
     * @param fields
     * @return
     */
    public static SimpleResponseBody<Object> success(String startId, String fields) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "成功", startId, fields, null);
    }

    /**
     * *  构造成功调用结果对象，设置返回消息"success"
     *
     * @param fields
     * @return
     */
    public static SimpleResponseBody<Object> success(String fields, Object data) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "成功", "", fields, data);
    }


    public static SimpleResponseBody<Object> success(String fields, Object data, ProfessionType professionType) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "成功", "成功",
                1, fields, null, null, false, data, professionType);
    }

    public static SimpleResponseBody<Object> success(String fields, Object data, ProfessionType professionType, String modelId) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "成功", "成功",
                1, fields, null, null, false, data, professionType, modelId);
    }

    public static SimpleResponseBody<Object> success(Map<String, String> fieldMap, Boolean isOrder, Object data) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "成功", "", fieldMap, isOrder, data, null);
    }

    public static SimpleResponseBody<Object> success(Map<String, String> fieldMap, Boolean isOrder, Object data, ProfessionType professionType) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "成功", "", fieldMap, isOrder, data, professionType);
    }

    /**
     * *  构造成功调用结果对象，设置返回消息"success"
     *
     * @param startId
     * @param fields
     * @return
     */
    public static SimpleResponseBody<Object> success(String startId, String fields, Object data) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.SUCCESS.getValue()), "成功", startId, fields, data);
    }

    /**
     * /
     * <b>功能</b>：<br/>将指定的字符串作为message创建一个标识操作失败的ResponseObject。
     *
     * @param fail message
     * @return ResponseObject
     */
    public static SimpleResponseBody<Object> fail(String fail) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.FAILURE.getValue()), fail);
    }

    public static SimpleResponseBody<Object> fail(String fail, ResponseCodeEnum enumCode) {
        return new SimpleResponseBody<Object>(String.valueOf(enumCode.getValue()), fail);
    }

    public static SimpleResponseBody<Object> fail(String message, String errors) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.FAILURE.getValue()), message, errors);
    }

    public static SimpleResponseBody<Object> fail(Object data) {
        return new SimpleResponseBody<Object>(String.valueOf(ResponseCodeEnum.FAILURE.getValue()), "失败", "", "", data);
    }

    public boolean isSuccessful() {
        return this.getStatus().equals(ResponseCodeEnum.SUCCESS.getValue());
    }

}
